package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.core.Result;
import com.campsys.core.TableData;
import com.campsys.domain.TCourse;
import com.campsys.domain.TManagerTeacher;
import com.campsys.domain.param.CourseAddParam;
import com.campsys.domain.param.CourseUpdateParam;
import com.campsys.domain.param.PageParam;
import com.campsys.domain.vo.CourseListVo;
import com.campsys.domain.vo.CoursePageVo;
import com.campsys.domain.vo.TeacherPageVo;
import com.campsys.mapping.CourseMapping;
import com.campsys.mapping.TeacherMapping;
import com.campsys.service.ICourseService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "课程管理")
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    @ApiOperation("添加课程")
    @PreAuthorize("@lcc.myAuthority('course:add')")
    @PostMapping("/add")
    public Result addCourse(@RequestBody CourseAddParam courseAddParam) {

        boolean rs = courseService.save(CourseMapping.INSTANCE.to(courseAddParam));

        return rs ? Result.success() : Result.error();

    }

    @ApiOperation("修改课程")
    @PreAuthorize("@lcc.myAuthority('course:update')")
    @PutMapping("/update")
    public Result updateCourse(@RequestBody CourseUpdateParam courseUpdateParam) {

        boolean rs = courseService.updateById(CourseMapping.INSTANCE.to(courseUpdateParam));

        return rs ? Result.success() : Result.error();

    }

    @ApiOperation("删除课程")
    @PreAuthorize("@lcc.myAuthority('course:delete')")
    @PutMapping("/delete/{id}")
    public Result deleteCourse(@PathVariable Long id) {

        TCourse tCourse = new TCourse();
        tCourse.setId(id);
        tCourse.setStatus("3");
        boolean rs = courseService.updateById(tCourse);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("开放课程")
    @PreAuthorize("@lcc.myAuthority('course:open')")
    @PutMapping("/open/{id}")
    public Result openCourse(@PathVariable Long id) {

        TCourse tCourse = new TCourse();
        tCourse.setId(id);
        tCourse.setStatus("1");
        boolean rs = courseService.updateById(tCourse);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("课程关联教学班")
    @PreAuthorize("@lcc.myAuthority('course:related:clazz')")
    @PutMapping("/related/clazz")
    public Result openCourse(Long courseId, Long clazzId) {

        TCourse tCourse = new TCourse();
        tCourse.setId(courseId);
        tCourse.setClassId(clazzId);
        boolean rs = courseService.updateById(tCourse);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("关闭课程")
    @PreAuthorize("@lcc.myAuthority('course:close')")
    @PutMapping("/close/{id}")
    public Result closeCourse(@PathVariable Long id) {

        TCourse tCourse = new TCourse();
        tCourse.setId(id);
        tCourse.setStatus("2");
        boolean rs = courseService.updateById(tCourse);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("查询课程分页列表")
    @PreAuthorize("@lcc.myAuthority('course:page')")
    @GetMapping("/page")
    public TableData getCoursePage(@Validated PageParam pageParam, String name, Long clazzId){

        return courseService.getCoursePage(pageParam,name,clazzId);

    }

    @ApiOperation("查询课程列表")
    @PreAuthorize("@lcc.myAuthority('course:list')")
    @GetMapping("/list")
    public Result getCourseList(Long teacherId){
        LambdaQueryWrapper<TCourse> qw = new LambdaQueryWrapper<>();
        qw.eq(TCourse::getTeacherId,teacherId);

        List<CourseListVo> list = CourseMapping.INSTANCE.toList(courseService.list(qw));
        return Result.success(list);

    }

}
